<?php
/**
 *============================================================================
 * Cornice PHP Framework & Website Builder
 * Freeware (f) 2011 by Inforo Software & Computer
 *============================================================================
 */
 
/**
 * Class contains miscellaneous utils
 * @author Nicola Dinh
 * @version 2011.04.2217
 */
class utils {

  /**
   * Default instance level constructor
   * Just decorative, should not be called due to having only static methods
   */
  public function __construct() {
    //
  }
  
  /**
   * Trim trailing & tailing characters, also extra spaces in between
   * @param string $text The text to trim
   * @return string The trimmed text
   */
  public static function trim_neat($text) {
    $text = trim($text);
    while (strpos($text,chr(32).chr(32))!==false)
      $text = str_replace(chr(32).chr(32),chr(32),$text);
    return $text;
  }
  
  /**
   * Convert object into multi-level array
   * @param object $object The object to convert
   * @return array The corresponding multi-level array to the object   
   */
  public static function obj_to_arr($object) {
    if (!is_object($object) && !is_array($object))
      return $object;
  
    //fun stuff
    if (is_object($object))
      $properties = get_object_vars($object);
    else
      $properties = $object;
    
    //convert
    $array = array();
    foreach ($properties as $name=>$value) 
      if (is_object($value) || is_array($value))
        $array[$name] = self::obj_to_arr($value);
      else
        $array[$name] = $value; 
        
    return $array;
  }//obj_to_arr
  
  /**
   * Get value from a certain path in multi-level array
   * @param array $array The multi-level array
   * @param string $arr_path The path in array, can not be blank
   * @return object The value of that array level (object or primitive)
   */
  public static function get_arr_value($array,$arr_path) {
    $arr_path = str_replace(chr(32),"",$arr_path);
    $tokens = explode("/",$arr_path);
    
    //find value
    $value = $array;
    foreach ($tokens as $token) 
      $value = $value[$token];
    
    //return
    return $value;
  }  
  
  /**
   * Get file extension 
   * @param string $file_path The path to a file
   * @return string The file extension in lowercase
   */
  public static function get_file_ext($file_path) {
    $parts = pathinfo($file_path);
    return $parts["extension"];
  }
  
  /**
   * Get file names in a directory
   * @param string $dir_path The path to directory
   * @return array The list of files (at least 2, '.' & '..')
   */
  public static function get_files_in($dir_path,$extension=null) {
    if (!file_exists($dir_path))
      return array();
    if ($extension!=null)
      $extension = strtolower($extension);
  
    $dir_handle = opendir($dir_path);
    if ($dir_handle===false)
      return array();
      
    $files = array();
    while (($file=readdir($dir_handle))!==false) {
      $file = strtolower($file);
      if ($extension==null)
        array_push($files,$file);    
      else
      if (self::get_file_ext($file)==$extension)
        array_push($files,$file);
    }
      
    closedir($dir_handle);
    return $files;
  }
}